package com.example.mgsnew.config;

import com.example.common.constants.MqConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// message/config/NotificationConfig.java
@Configuration
@Slf4j
public class NotificationConfig {
    // 广播交换机
    public static final String EXCHANGE_NOTIFICATION = MqConstants.EXCHANGE_NOTIFICATION;

    // 教师通知队列
    public static final String QUEUE_SAVE = MqConstants.QUEUE_SAVE;
    public static final String QUEUE_BROADCAST = MqConstants.QUEUE_BROADCAST;


    // 学生通知队列
    public static final String QUEUE_STUDENT_NOTIFICATION = MqConstants.QUEUE_STUDENT_NOTIFICATION;

    //交换机在服务器重启后仍然存在,即使没有队列绑定，交换机也不会自动删除
    @Bean
    public FanoutExchange notificationExchange() {
        return new FanoutExchange(EXCHANGE_NOTIFICATION, true, false);
    }

    //队列在服务器重启后仍然存在
    @Bean
    public Queue teacherNotificationSaveQueue() {
        log.info("老师消息通知保存队列初始化！");
        return new Queue(QUEUE_SAVE, true);
    }

    @Bean
    public Queue studentNotificationQueue() {
        log.info("学生消息队列初始化！");
        return new Queue(QUEUE_STUDENT_NOTIFICATION, true);
    }
    @Bean
    public Queue teacherNotificationBroadcastQueue() {
        log.info("老师消息web发布保存队列初始化！");
        return new Queue(QUEUE_BROADCAST, true);
    }

    @Bean
    public Binding teacherNotificationSaveBinding() {
        return BindingBuilder.bind(teacherNotificationSaveQueue())
                .to(notificationExchange());
    }


    @Bean
    public Binding teacherNotificationBroadcastBinding() {
        return BindingBuilder.bind(teacherNotificationBroadcastQueue())
                .to(notificationExchange());

    }

    @Bean
    public Binding studentNotificationBinding() {
        return BindingBuilder.bind(studentNotificationQueue())
                .to(notificationExchange());
    }
}
